﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyHealthByDesignWebsite.Models;

namespace MyHealthByDesignWebsite.Controllers
{
    public class MyHealthMeasurementsController : Controller
    {
        //Description:  Use to get and set Health Measure model data using the related view
        //Created:  Nov 2011
        //Programmer:  Bob Whiteis

        NutritionProfileRepository npRepository = new NutritionProfileRepository();
        HealthProfileRepository hpRepository = new HealthProfileRepository();
        //
        // GET: /MyHealthMeasurements/

        public ActionResult Index()
        {
            return View();
        }


        //
        // POST: /MyHealthMeasurements/
        //pre-condition: UserID has been saved in cookie
        //post-condition:  Health Profile and Nutrition profile models have been create for the user
        //post-condition: User has been redirect to the inital Health Measure Page
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            int userID = Convert.ToInt32(Request.Cookies["UserID"].Value);
           
            // if user has completed my health measure section
            // redirect to 
            if (MyHeathMeasurementsComplete(userID))
                return RedirectToAction("Create", "MyHealthHistory");

            NutritionProfile np = new NutritionProfile()
            {
                U_UserID = userID,
                NP_entryDate = DateTime.Now
            };

            HealthProfile hp = new HealthProfile()
            {
                U_UserID = userID,
                HP_entryDate = DateTime.Now
            };

            MyHealthMeasModelView hm = new MyHealthMeasModelView(np, hp);
            ActivityLevel al = new ActivityLevel();
            //get activity level for drop down list
            ViewBag.AL_activityLevelID = al.items();
            return View("Create", hm);
        }


        //
        // GET: /MyHealthMeasurements/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /MyHealthMeasurements/Create
        //pre-condition: UserID has been saved in cookie
        //post-condition:  View has been displayed to allow used to enter nutritionProfile and Health Measure information
        public ActionResult Create()
        {

            NutritionProfile np = new NutritionProfile()
            {
               
                NP_entryDate = DateTime.Now
            };

            HealthProfile hp = new HealthProfile()
            {
                
                HP_entryDate = DateTime.Now
            };

            MyHealthMeasModelView hm = new MyHealthMeasModelView(np, hp);

            ActivityLevel al = new ActivityLevel();
            //get activity level for drop down list
            ViewBag.AL_activityLevelID = al.items();

            return View(hm);
        } 

        //
        // POST: /MyHealthMeasurements/Create
        //pre-condition: UserID has been saved in cookie
        //post-condition: Nutrition Profile and Health Measure information information entered by the user has been saved
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            NutritionProfile np = new NutritionProfile();
            HealthProfile hp = new HealthProfile();
            MyHealthMeasModelView hm = new MyHealthMeasModelView(np, hp);
            try
            {
                UpdateModel(hm);


                np = hm.np;
                hp = hm.hp;
   
                npRepository.Add(np);
                hpRepository.Add(hp);
                npRepository.Save();
                hpRepository.Save();

                return RedirectToAction("Create", "MyHealthHistory");
            }
            catch
            {
                foreach (var issue in hm.np.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }

                ActivityLevel al = new ActivityLevel();
                //get activity level for drop down list
                ViewBag.AL_activityLevelID = al.items();

                return View(hm);
            }
        }
        
        //
        // GET: /MyHealthMeasurements/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /MyHealthMeasurements/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /MyHealthMeasurements/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /MyHealthMeasurements/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //returns true if the user has completed my health measures
        //pre-condition:  Nutrition Profile has been create for the user
        //post-condtion:  True have been return if the nutrition profile is complete
        public bool MyHeathMeasurementsComplete(int userID)
        {
            FullDataSetDataContext db = new FullDataSetDataContext();
            if (db.NutritionProfiles.Any(n => n.U_UserID == userID))
                return true;
            else
                return false;
        }

        //returns BMI
        //pre-conditon:  User height and wieght have been entered
        //post-condisiton: The user BMI has been returned
        public JsonResult BMI(string weight, string heightFt, string heightIn) 
        {
            double dblWeight;
            string bmi = " ";
            if (!Double.TryParse(weight, out dblWeight))
                return Json(bmi);
            int intHeightFt = Convert.ToInt32(heightFt);
            int intHeightIn = Convert.ToInt32(heightIn) + (intHeightFt * 12);
            double dblBMI = intHeightIn *  intHeightIn;
            dblBMI = dblWeight / dblBMI * 703;
            
            bmi = String.Format("{0:0.0}", dblBMI) + " kg/m<sup>2</sup>";
            return Json(bmi);
            

        }
    }
}
